08. This Pointer
This Pointer
When working with classes it is often helpful to be able to refer to the current class instance or object. For example, given the following Car
class from a previous lesson, the IncrementDistance()
method implicitly refers to the current Car
instance's distance
attribute:
// The Car class
class Car {
public:
// Method to print data.
void PrintCarData() {
cout << "The distance that the " << color << " car " << number << " has traveled is: " << distance << "\n";
}
// Method to increment the distance travelled.
void IncrementDistance() {
distance++;
}
// Class/object attributes
string color;
int distance = 0;
int number;
};
It is possible to make this explicit in C++ by using the this
pointer, which points to the current class instance. Using this
can sometimes be helpful to add clarity to more complicated code:
// The Car class
class Car {
public:
// Method to print data.
void PrintCarData() {
cout << "The distance that the " << this->color << " car " << this->number << " has traveled is: " << this->distance << "\n";
}
// Method to increment the distance travelled.
void IncrementDistance() {
this->distance++;
}
// Class/object attributes
string color;
int distance = 0;
int number;
};
You may see this
used in some code in the remainder of the course, and in the next exercise, you will want to use this
as an argument to a function. Click to the next concept to get started!